package com.hb.trade.api.routine.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.Attachments;
import org.fzzn.core.util.IdUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件工具类
 *
 * @author zhaojk
 */
public class TradeFileUtil {

    /**
     * 将一个文件持久化到服务器上
     *
     * @param files
     * @return
     * @deprecated jnpf方法
     */
    @Deprecated
    public static List<Map<String, Object>> stay(File... files) {
        if (files == null || files.length == 0) {
            return null;
        }
        List<Map<String, Object>> maps = new ArrayList<>();
        for (File file : files) {
            Map<String, Object> item = new HashMap<>();
            String fileName = file.getName();

            item.put("name", fileName);

            String fileId = IdUtil.getIdStr();
            item.put("fileId", fileId);

            item.put("url", "");
            item.put("uid", DateUtil.date().getTime());
            item.put("status", "success");

            // 转移文件
            File copyFile = FileUtil.copyFile(file.getAbsolutePath(), "/" + fileId);
            if (copyFile.exists()) {
                maps.add(item);
                continue;
            }
            throw new RuntimeException("持久化文件时后台异常");
        }
        return maps;
    }

    /**
     * 将文件地址解析成文件
     *
     * @param attachments 如果有多个文件只下载第一个
     * @return
     */
    public static File parseFile(Attachments attachments) {
        return parseFile(JSONUtil.toJsonStr(attachments));
    }

    /**
     * 将一个字符串转换成文件
     *
     * @param fileInfoStr 字符串可以支持http或https开头的远程文件地址, 还可以是 Attachments对象格式的json字符串, 如果有多个文件只下载第一个
     * @return
     * @see Attachments
     */
    public static File parseFile(String fileInfoStr) {
        if (StrUtil.isEmpty(fileInfoStr)) {
            return null;
        }
        String fileUrl;

        // http请求直接下载
        if (HttpUtil.isHttp(fileInfoStr) || HttpUtil.isHttps(fileInfoStr)) {
            fileUrl = fileInfoStr;
            return getRemoteFile(fileUrl);
        }

        // JSON对象对象需要解析
        if (JSONUtil.isJsonObj(fileInfoStr)) {
            Attachment attachment = JSONUtil.toBean(fileInfoStr, Attachment.class);
            return attachment.getThisFile();
        }

        // 数组只下载第一个
        if (JSONUtil.isJsonArray(fileInfoStr)) {
            JSONArray array = JSONUtil.parseArray(fileInfoStr);
            Attachments attachments = array.toBean(Attachments.class);
            Attachment attachment = attachments.get(0);
            return attachment.getThisFile();
        }
        return null;
    }

    /**
     * 将远程文件下载到本地
     *
     * @param fileUrl http开头的文件地址
     * @return
     */
    public static File getRemoteFile(String fileUrl) {
        String name = FileUtil.getName(fileUrl);
        File file = new File("temps/" + name);
        HttpUtil.downloadFile(fileUrl, file);
        return file;
    }
}
